package com.example.demo.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.UUIDUtils;
import com.example.demo.entity.MenuEntity;
import com.example.demo.entity.OperationLogEntity;
import com.example.demo.exception.BizException;
import com.example.demo.mapper.IMenuMapper;
import com.example.demo.service.IMenuService;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<IMenuMapper, MenuEntity> implements IMenuService {

	@Autowired
	public IMenuMapper menuMapper;
	
	public List<Map<String,Object>> queryMenuList(Map<String,Object> whereMap) throws BizException{
		List<Map<String,Object>> result=null;
		try {
			
			String name=(String)whereMap.get("name");			
			result = menuMapper.selectMenuList(whereMap);			
			log.debug("查询系统菜单："+whereMap); 		
			
		} 
		catch (Exception e) {
			// TODO: handle exception
			log.error("查询系统菜单："+whereMap,e); 
			throw new BizException("查询系统菜单出错:"+e.getMessage(),e);
		}
		
		return result;
	}
	
	@Transactional(rollbackFor = Exception.class)
	public String AddNewMenu(MenuEntity menuEntity, OperationLogEntity operationLogEntity) throws BizException {
		String result="";
		
		try {			
			//添加菜单
			menuEntity.setId(UUIDUtils.NewGUID());
			menuEntity.insert();
			//添加操作日志
			operationLogEntity.setOperatDateTime(new Date()); 
			operationLogEntity.insert();
		}
		catch (Exception ex) {
			// TODO: handle exception
			throw new BizException("添加菜单时出错"+ex.getMessage(), ex);
		}
		
		return result;
	}
	
}
